

********************************************************************************
*This file takes the raw, session level, data files and converts them into 
*something more usable. Proceeding in three steps:
*1. Take each raw data file and save it as a Stata file.
*2. Combine the session level Stata files into one State file pre treatment.
*3. Clean the data up a bit. 
********************************************************************************


********************************************************************************
*Input files
* enforcement_EV_session_1
* enforcement_EV_session_2
* enforcement_EV_session_3
* enforcement_EV_session_4
* enforcement_Comb_session_1
* enforcement_Comb_session_2
* enforcement_Comb_session_3
* enforcement_Comb_session_4
* enforcement_Comb_session_5
* enforcement_Comb_session_6
* survey_EV_session_1
* survey_EV_session_2
* survey_EV_session_3
* survey_EV_session_4
* survey_Comb_session_1
* survey_Comb_session_2
* survey_Comb_session_3
* survey_Comb_session_4
* survey_Comb_session_5
* survey_Comb_session_6
********************************************************************************


********************************************************************************
*Intermediate ouptuts
* session_1_EV
* session_2_EV
* session_3_EV
* session_4_EV
* session_1_Comb
* session_2_Comb
* session_3_Comb
* session_4_Comb
* session_5_Comb
* session_6_Comb
* survey_session_1_EV
* survey_session_2_EV
* survey_session_3_EV
* survey_session_4_EV
* survey_session_1_Comb
* survey_session_2_Comb
* survey_session_3_Comb
* survey_session_4_Comb
* survey_session_5_Comb
* survey_session_6_Comb
********************************************************************************



********************************************************************************
*Final outputs
* EV_data_4_sessions
* Comb_data_6_sessions
********************************************************************************




clear all
set more off

import delimited survey_EV_session_1
save survey_session_1_EV, replace
clear all
import delimited survey_EV_session_2
save survey_session_2_EV, replace
clear all
import delimited survey_EV_session_3
save survey_session_3_EV, replace
clear all
import delimited survey_EV_session_4
save survey_session_4_EV, replace
clear all
import delimited survey_Comb_session_1
save survey_session_1_Comb, replace
clear all
import delimited survey_Comb_session_2
save survey_session_2_Comb, replace
clear all
import delimited survey_Comb_session_3
drop if sessioncode == "zrtsmif9"  //some artefacts from a test session are in this file. This command drops two empty rows of data.
save survey_session_3_Comb, replace
clear all
import delimited survey_Comb_session_4
save survey_session_4_Comb, replace
clear all
import delimited survey_Comb_session_5
save survey_session_5_Comb, replace
clear all
import delimited survey_Comb_session_6
save survey_session_6_Comb, replace




clear all
import delimited enforcement_EV_session_1
merge m:1 participantid_in_session using survey_session_1_EV, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_1_EV, replace
clear all
import delimited enforcement_EV_session_2
merge m:1 participantid_in_session using survey_session_2_EV, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_2_EV, replace
clear all
import delimited enforcement_EV_session_3
merge m:1 participantid_in_session using survey_session_3_EV, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_3_EV, replace
clear all
import delimited enforcement_EV_session_4
merge m:1 participantid_in_session using survey_session_4_EV, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_4_EV, replace
clear all
import delimited enforcement_Comb_session_1
merge m:1 participantid_in_session using survey_session_1_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_1_Comb, replace
clear all
import delimited enforcement_Comb_session_2
merge m:1 participantid_in_session using survey_session_2_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_2_Comb, replace
clear all
import delimited enforcement_Comb_session_3
merge m:1 participantid_in_session using survey_session_3_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_3_Comb, replace
clear all
import delimited enforcement_Comb_session_4
merge m:1 participantid_in_session using survey_session_4_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_4_Comb, replace
clear all
import delimited enforcement_Comb_session_5
merge m:1 participantid_in_session using survey_session_5_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_5_Comb, replace
clear all
import delimited enforcement_Comb_session_6
merge m:1 participantid_in_session using survey_session_6_Comb, keepusing(playerage playergender playermajor playerecon_classes playeryear playergpa)
save session_6_Comb, replace


clear all
use session_1_EV
append using session_2_EV
append using session_3_EV
append using session_4_EV

gen session = 0
replace session = 1 if sessioncode == "8endm974"
replace session = 2 if sessioncode == "pnwn59kz"
replace session = 3 if sessioncode == "2ta2hvfg"
replace session = 4 if sessioncode == "inn85kvp"

drop participantcode participantlabel participant_is_bot participant_index_in_pages
drop participant_max_page_index participant_current_app_name participant_current_page_name
drop participantip_address 
drop participanttime_started participantvisited 
drop participantmturk_assignment_id participantmturk_worker_id
drop playerquiz_question_1 playerquiz_question_2 playerquiz_question_3
drop groupcolor_drawn sessioncode sessionlabel sessionexperimenter_name
drop sessionmturk_hitid sessionmturk_hitgroupid sessioncomment sessionis_demo

gen stata_id = participantid_in_session + session*100
drop participantid_in_session


rename playerhigh_monitor m_a
label variable m_a m_A
rename playerlow_monitor m_b
label var m_b m_B
rename playerpayoff payoff
rename playerid_in_group role
rename subsessionis_arrest_round revenue_round
rename playertask_choice task_choice
rename groupis_observed is_observed
rename subsessionis_option is_option
rename subsessionround_number round
rename groupid_in_subsession group
rename playerage age
rename playerecon_classes num_econ_classes
rename playergender gender
encode gender, gen(gend)
drop gender
rename gend gender
rename playergpa gpa
rename playermajor major
encode major, gen(maj)
drop major
rename maj major
rename playeryear year_of_degree
encode year_of_degree, gen(yod)
drop year_of_degree
rename yod year_of_degree


//Create indicator variables for driver choice
gen chose_A = 0 if role == 2
replace chose_A = 1 if task_choice == 1
gen chose_B = 0 if role == 2
replace chose_B = 1 if task_choice == 2
gen chose_C = 0 if role == 2
replace chose_C = 1 if task_choice == 3

save EV_data_4_sessions, replace
clear all 



use session_1_Comb
append using session_2_Comb
append using session_3_Comb
append using session_4_Comb
append using session_5_Comb
append using session_6_Comb
gen session = 0
replace session = 1 if sessioncode == "48sw4tgk"
replace session = 2 if sessioncode == "1p62szhf"
replace session = 3 if sessioncode == "2i3b30qw"
replace session = 4 if sessioncode == "l9iukv91"
replace session = 5 if sessioncode == "cf29ajwb"
replace session = 6 if sessioncode == "47vyjugl"

drop participantcode participantlabel participant_is_bot participant_index_in_pages
drop participant_max_page_index participant_current_app_name participant_current_page_name
drop participantip_address 
drop participanttime_started participantvisited 
drop participantmturk_assignment_id participantmturk_worker_id
drop playerquiz_question_1 playerquiz_question_2 playerquiz_question_3
drop groupcolor_drawn sessioncode sessionlabel sessionexperimenter_name
drop sessionmturk_hitid sessionmturk_hitgroupid sessioncomment sessionis_demo

gen stata_id = participantid_in_session + session*100 + 1000
drop participantid_in_session


rename playerhigh_monitor m_a
label variable m_a m_A
rename playerlow_monitor m_b
label var m_b m_B
rename playerpayoff payoff
rename playerid_in_group role
rename subsessionis_arrest_round revenue_round
rename playertask_choice task_choice
rename groupis_observed is_observed
rename subsessionis_option is_option
rename subsessionround_number round
rename groupid_in_subsession group
rename playerage age
rename playerecon_classes num_econ_classes
rename playergender gender
encode gender, gen(gend)
drop gender
rename gend gender
rename playergpa gpa
rename playermajor major
encode major, gen(maj)
drop major
rename maj major
rename playeryear year_of_degree
encode year_of_degree, gen(yod)
drop year_of_degree
rename yod year_of_degree


//Create indicator variables for driver choice
gen chose_A = 0 if role == 2
replace chose_A = 1 if task_choice == 1
gen chose_B = 0 if role == 2
replace chose_B = 1 if task_choice == 2
gen chose_C = 0 if role == 2
replace chose_C = 1 if task_choice == 3

save Comb_data_6_sessions, replace






